package com.ash.service.type;

import com.ash.domain.Type;
import com.ash.mapper.type.TypeMapper;
import com.ash.util.PojoUtil;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author : Chen Chao
 * @encoding : UTF-8
 * @date : 2021/3/25 16:49
 * @file : project06.com.ash.service.type.TypeServiceImpl.java
 * @software : IntelliJ IDEA
 * @since : 1.8
 */
@Service
public class TypeServiceImpl implements TypeService {
    @Resource
    TypeMapper typeMapper;
    @Override
    public int add(Type type) {
        return typeMapper.add(type);
    }

    @Override
    public int deleteById(int tid) {
        return typeMapper.deleteById(tid);
    }

    @Override
    public int deleteByIds(List<Integer> ids) {
        if(ids.size()>0){
            return typeMapper.deleteByIds(ids);
        }
        return 0;
    }

    @Override
    public List<Type> selectAll(@Nullable Integer page, @Nullable Integer limit) {
        if(page!=null&&limit!=null){
            return typeMapper.selectAll((page-1)*limit,limit);
        }
        return  typeMapper.selectAll(null,null);
    }

    @Override
    public int dataCount() {
        return typeMapper.dataCount();
    }

    @Override
    public int add(String tname) {
        List<Type> types = typeMapper.selectByTypeName(tname);
        if(types.size()==0){
            Type type = new Type();
            type.setTname(tname);
            return typeMapper.add(type);
        }else {
            return 0;
        }
    }

    @Override
    public int update(Type type) {
        Integer tid = type.getTid();
        if(tid==null){
            return 0;
        }
        else {
            return typeMapper.update(PojoUtil.beanToMap(type).entrySet(),tid);
        }
    }

}
